package com.buddy.summary.code.algorithm;

import java.util.Stack;

//利用栈实现队列
public class Algorithm_06 {

    private Stack<Integer> stackA = new Stack<>();
    private Stack<Integer> stackB = new Stack<>();

    //入队操作
    public void enQueue(int ele){
        stackA.push(ele);
    }
    //出队操作
    public Integer deQueue(){
        if (stackB.isEmpty()){
            if (stackA.isEmpty())
                return null;
            transfer();
        }
        return stackB.pop();
    }

    private void transfer() {
        while (!stackA.isEmpty())
            stackB.push(stackA.pop());
    }

    public static void main(String[] args) {
        Algorithm_06 stackQueue = new Algorithm_06();
        stackQueue.enQueue(1);
        stackQueue.enQueue(2);
        stackQueue.enQueue(3);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());
        stackQueue.enQueue(4);
        System.out.println(stackQueue.deQueue());
        stackQueue.enQueue(5);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());
    }
}
